package com.wx.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.HashMap;

/**
 * 结果返回
 * Created by king-pc on 2018/12/4.
 */
public class R {

    /**
     * 操作成功返回
     * @param obj
     * @return json字符串
     */
    public static String ok(Object obj) {
        ResponseObject responseObject= ResponseObject.newSuccessResponseObject(obj);
        //设置空对象输出 SerializerFeature.WriteMapNullValue,
        return JSON.toJSONString(responseObject,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 操作成功返回
     * @return json字符串
     */
    public static String ok() {
        ResponseObject responseObject= ResponseObject.newSuccessResponseObject(true);
        return JSON.toJSONString(responseObject,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 操作异常
     * @return
     */
    public static String error(){
        ResponseObject responseObject= ResponseObject.newErrorResponseObject(ResponseObject.status_400, "操作异常");
        return JSON.toJSONString(responseObject,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 操作异常
     * @param status 异常代码
     * @param errorMessage 错误描述
     * @return
     */
    public static String error(Integer status,String errorMessage){
        ResponseObject responseObject= ResponseObject.newErrorResponseObject(status, errorMessage);
        return JSON.toJSONString(responseObject,SerializerFeature.WriteMapNullValue);
    }


    public static void main(String[] arg){
        System.out.println(R.ok("操作成功"));
        System.out.println(R.ok());
        System.out.println(R.error());
        System.out.println(R.error(ResponseObject.status_500,"未知异常"));
    }
}
